<template>
  <Select ref="driverSelectComp" placeholder="请选择驾驶员" :value="value" :multiple="isMultiple" @on-change="change" filterable clearable>
    <Option v-for="(option, index) in driverOptions" :value="option.value" :key="`driver-select-option-${option.value}`">{{option.label}}</Option>
  </Select>
</template>

<script>
  import {post}  from '@/util/request'

  export default {
    props: ["value",'multiple','orgId'],
    data() {
      return {
        driverOptions: []
      }
    },
    mounted(){
      this.queryDriver(this.orgId);
    },
    watch: {
      orgId(v){
        this.queryDriver(v);
      }
    },
    computed:{
      isMultiple(){
        return this.multiple !== undefined && this.multiple !== null && this.multiple;
      }
    },
    methods:{
      change(v){
        this.$emit("input", v);
        this.$emit("on-change", v);
      },
      queryDriver(orgId){
        let query = { orgIds:orgId||'' };
        post('/common/findDriverByOrgId', query).then(json => {
          if (json.data.success) {
            this.driverOptions = json.data.data.map(item => {
              return {
                value: item.id,
                label: item.name
              };
            });
          }
        });
      },
      setQuery(v){
        this.$refs.driverSelectComp.setQuery(v);
      }
    }
  }
</script>
